import pymysql

def query(sql,*args):
    """
    封装查询
    :param sql:
    :param args:
    :return:
    """

    try:

        conn = pymysql.connect(host='localhost',
                               user='zyh',
                               password='123456',
                               db='cov',
                               charset='utf8'
                               )
        # 创建游标，默认元组
        cursor = conn.cursor()
        cursor.execute(sql,args)
        res = cursor.fetchall()
        return res
    except:
        print('连接数据库失败！')
    finally:

        cursor.close()
        conn.close()
        print('数据库关闭成功！')



def get_leiji():
    """
    返回四个累计数据
    :return:
    """
    sql = "select nowConfirm,confirm,dead,heal " \
          "from leiji where dat=" \
          "(select dat from leiji order by dat desc limit 1)"
    res = query(sql)
    return res[0]


def sql_search(ss):
    """
    完成搜索框
    :return:
    """
    sql = "select city,confirm from details where provice='{}'".format(ss)
    re = query(sql)
    new_re = []
    p = list(re)
    for x in p:
        i = list(x)
        i[0] = i[0]+'市'
        new_re.append((i[0],x[1]))
    return tuple(new_re)

def das():
    """
    打赏人数
    :return:
    """
    re = query("select dasnum from world where id=1")
    print(re[0][0])
    return re[0]